package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.List;
import java.util.Objects;

import com.google.common.collect.Lists;
import com.iwdnb.gkgz.infrastructure.dao.entity.StockDO;
import com.iwdnb.gkgz.infrastructure.dao.entity.StockDayDataDO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK;
import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_DAY;

@Component
public class StockRepositoryImpl implements StockRepository {

    @Autowired
    private MetaDataDAO metaDataDAO;

    @Override
    public StockDO findByCode(String code) {
        List<StockDO> stockDOList = findByCodeIn(Lists.newArrayList(code));
        if (CollectionUtils.isEmpty(stockDOList)) {
            return null;
        }
        return stockDOList.get(0);
    }

    @Override
    public List<StockDO> findByCodeIn(List<String> codes) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("codeList", codes);
        return metaDataDAO.queryListByCondition(OBJECT_CODE_STOCK, metaQuery, StockDO.class);
    }

    @Override
    public void saveAll(List<StockDO> stockDOList) {
        for (StockDO stockDO : stockDOList) {
            save(stockDO);
        }
    }

    @Override
    public void save(StockDO stockDO) {
        if (Objects.isNull(stockDO.getId())) {
            metaDataDAO.insert(OBJECT_CODE_STOCK, stockDO);
        } else {
            metaDataDAO.update(OBJECT_CODE_STOCK, stockDO);
        }
    }

}
